<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">点赞信息</div>
                <div class="layui-card-body" pad15>
                    <div class="layui-form layui-form-pane" lay-filter="{$lay_table}">
                        <input type="hidden" name="id" value="{:array_get($info,'id')}">
                        <div class="layui-form-item">
                            <label class="layui-form-label"><span style="color:#f90c05;">*</span>推广日期</label>
                            <div class="layui-input-block">
                                <input type="text" name="promotion_time" lay-verify="required" id="promotion_time" value="{:array_get($info,'promotion_time')}" placeholder="请选择推广日期" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label"><span style="color:#f90c05;">*</span>操作人</label>
                            <div class="layui-input-block">
                                <input type="text" name="operator" value="{:array_get($info,'operator')}" lay-verify="required"
                                       placeholder="请输入操作人" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item" pane>
                            <label class="layui-form-label"><span style="color:#f90c05;">*</span>状态</label>
                            <div class="layui-input-block">
                                {volist name="status_list" id="v"}
                                <input type="radio" name="status" value="{$key}" title="{$v}" {eq name=":array_get($info,'status',2)" value="$key" }checked{/eq}>
                                {/volist}
                            </div>
                        </div>
                        <div class="layui-form-item" pane>
                            <label class="layui-form-label">备注</label>
                            <div class="layui-input-block">
                                <textarea name="comment" placeholder="请输入备注"
                                          class="layui-textarea">{:array_get($info,'comment')}</textarea>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div>
                                <button class="layui-btn layuiadmin-btn" id="add-table" type="button">添加</button>
                                <button class="layui-btn layuiadmin-btn" id="finish-table" type="button" data-index="${k}">计算结果</button>
                            </div>
                            <form id="form">
                                <table class="layui-table">
                                    <colgroup>
                                        <col width="200">
                                        <col width="200">
                                        <col width="200">
                                        <col width="200">
                                        <col width="200">
                                        <col width="200">
                                        <col width="150">
                                    </colgroup>
                                    <thead>
                                    <tr>
                                        <th>完成点赞</th>
                                        <th>生效点赞</th>
                                        <th>推广人收益</th>
                                        <th>实际收益</th>
                                        <th>扣除推广人后</th>
                                        <th>备注</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody id="tb">

                                    </tbody>
                                </table>
                            </form>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <input type="button" lay-submit lay-filter="{$lay_btn}" value="确认提交" class="layui-btn">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
\
<script>
    layui.use(['index', 'form', 'upload', 'laydate'], function () {
        const $ = layui.$;
        const admin = layui.admin;
        const form = layui.form;
        const laydate = layui.laydate;
        form.render();

        laydate.render({
            elem: '#promotion_time',
            type: 'date'
        });

        form.on('submit({$lay_btn})', function (obj) {
            const index = parent.layer.getFrameIndex(window.name);
            admin.req({
                url: "{:url('dy/editDianzan')}",
                type: 'post',
                data: obj.field,
                done: function (res) {
                    layer.msg("提交成功", {
                        icon: 1
                    });
                    parent.layui.table.reload('lay-dy-dianzan-table'); //重载表格
                    parent.layer.close(index);
                }
            });
        });

        let data = {:array_get($info,'promotion_list','[]')};
        const gt2000 = {$gt2000};
        const lt2000 = {$lt2000};
        const profit = {$profit};
        function createTable() {
            let html = "";
            data.forEach((v, k) => {
                html += `<tr>
                        <td>
                            <input placeholder="完成点赞数"
                                   name="promotion_list[${k}][finish]"
                                   lay-verify="required"
                                   value="${v.finish}"
                                   data-k="${k}"
                                   class="layui-input finish-input"/>
                        </td>
                        <td>
                            <input placeholder="生效点赞数"
                                   name="promotion_list[${k}][valid]"
                                   lay-verify="required"
                                   value="${v.valid}"
                                   readonly
                                   class="layui-input"/>
                        </td>
                        <td>
                            <input placeholder="推广人收益"
                                   type="number"
                                   name="promotion_list[${k}][income]"
                                   lay-verify="required"
                                   value="${v.income}"
                                   readonly
                                   class="layui-input"/>
                        </td>
                        <td>
                            <input placeholder="实际收益"
                                   name="promotion_list[${k}][ac_income]"
                                   lay-verify="required"
                                   value="${v.ac_income}"
                                   readonly
                                   class="layui-input"/>
                        </td>
                        <td>
                            <input placeholder="扣除推广后的收益"
                                   name="promotion_list[${k}][de_income]"
                                   lay-verify="required"
                                   value="${v.de_income}"
                                   readonly
                                   class="layui-input"/>
                        </td>
                        <td>
                            <input placeholder="备注"
                                   name="promotion_list[${k}][comment]"
                                   value="${v.comment}"
                                   class="layui-input"/>
                        </td>
                        <td>
                            <button class="layui-btn layui-btn-danger layuiadmin-btn delete-table" type="button" data-index="${k}">删除</button>
                        </td>
                </tr>`;
            });

            $('#tb').html(html);
        }

        createTable();
        $('#add-table').click(function () {
            data = getFormData();
            data.push({finish: "", valid: "", income: "", ac_income: "", de_income: "", comment:""});
            createTable();
        });

        $("#tb").on('click', '.delete-table', function () {
            let index = $(this).data('index');
            if (data.length <= 1) {
                return false;
            }

            data = getFormData();
            data.splice(index,1);
            createTable();
        });

        $("#tb").on('input', '.finish-input', function () {
            let k = $(this).data('k');
            data[k].finish = $(this).val();
        });

        $("#finish-table").click(function () {
            for (let k in data) {
                data[k].valid = Math.floor((data[k].finish - 0) * 0.75);
                if (data[k].valid >= 2000) {
                    data[k].income = Math.floor(data[k].valid * 100 * gt2000) / 100;
                } else {
                    data[k].income = Math.floor(data[k].valid * 100 * lt2000) / 100;
                }
                data[k].ac_income = Math.floor(data[k].valid * 100 * profit) / 100;
                data[k].de_income = (data[k].ac_income - data[k].income).toFixed(2);
            }

            createTable();
        });

        function getFormData() {
            let arr = $('#form').serializeArray();
            let obj = {};
            arr.forEach(v => {
                obj[v.name] = v.value;
            });

            // 提取键名中的索引和属性名
            let keys = Object.keys(obj).map(key => {
                let parts = key.match(/list\[(\d+)\]\[(\w+)\]/);
                return { index: parseInt(parts[1], 10), prop: parts[2] };
            });

            // 根据索引分组属性
            let grouped = {};
            keys.forEach(({ index, prop }, i) => {
                if (!grouped[index]) grouped[index] = {};
                grouped[index][prop] = obj[`promotion_list[${index}][${prop}]`];
            });

            // 将分组后的对象转换为数组
            let res = Object.keys(grouped).map(index => grouped[index]);

            for (let i in res) {
                for (let j in res[i]) {
                    if (res[i][j] === undefined) {
                        res[i][j] = "";
                    }
                }
            }

            return res;
        }
    });
</script>
